<?php
class Controller_Purchase_Loss extends Controller_Abstract
{
	function actionCreate()
	{
		$this->_view['title'] = '创建';
	}
	
	function actionCreateSave()
	{
		$this->_view['title'] = '创建（保存）';
		if ($this->_context->isPOST())
		{
			$key_arr = array();
			foreach ($_POST as $key => $p)
			{
				if (substr($key, 0, 11) == 'product_id_')
				{
					$key_arr[] = substr($key, 11, strlen($key) - 11);
				}
			}
			foreach ($key_arr as $k)
			{
				$temp = array();
				$temp['product_basic_id'] = $_POST['product_id_' . $k];
				$temp['price'] = $_POST['price_' . $k];
				$temp['quantity'] = abs(intval($_POST['quantity_' . $k]));
				$temp['reason'] = $_POST['reason_' . $k];
				$purchase_loss[$k] = $temp;
			}
			$result = Purchase_Loss::create($purchase_loss);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('添加成功', '正在返回..', url('Purchase::Loss/LossList'), $result['ack']);
			}
		}
	}
	
	function actionLossList()
	{
		$this->_view['title'] = '列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
		$user = Purchase_Order::getUser();
		$this->_view['user'] = $user['data'];
		$url_arr = Helper_BSS_Normal::buildCondition();
		$url_arr['limit'] = $user['ack'];
		$result = Purchase_Loss::search($url_arr, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Page($result['pagination'], url('Purchase::Loss/LossList', $url_arr));
			$this->_view['page'] = $page->getPage();
			$purchase_loss = $result['data'];
			Helper_BSS_Normal::getProductImgUrl($purchase_loss);
			$this->_view['purchase_loss'] = $purchase_loss;
		}
	}
	
	function actionLossEdit()
	{
		$this->_view['title'] = '修改（操作）';
		if ($_GET['purchase_loss_id'])
		{
			$purchase_loss = Purchase_Loss::find('purchase_loss.id=?', $_GET['purchase_loss_id'])
				->joinLeft('product_basic', 'number, name', 'product_basic.id=purchase_loss.product_basic_id')
				->joinLeft('user', 'name as create_user_name', 'user.id=purchase_loss.create_user_id')
				->asArray()->getOne();
			$this->_view['purchase_loss'] = $purchase_loss;			
		}
	}
	
	function actionLossEditSave()
	{
		$this->_view['title'] = '修改（保存）';
		if ($_POST['purchase_loss_id'])
		{
			$purchase_loss = array(
				'id' => $_POST['purchase_loss_id'],
				'product_basic_id' => $_POST['product_id'],
				'price' => $_POST['price'],
				'quantity' => $_POST['quantity'],
				'reason' => $_POST['reason']
			);
			$result = Purchase_Loss::edit($purchase_loss);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('修改成功', '正在返回..', url('Purchase::Loss/LossList'), $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('修改失败', $result['message'], url('Purchase::Loss/LossList'), $result['ack']);
			}
		}
	}
	
	function actionProductInfoAjax()
	{
		$this->_view['title'] = '产品信息请求ajax';
		$product_basic = Product_Basic::search(array('number_or_name' => $_REQUEST['q']), 0, 10);
		$result = array();
		foreach ($product_basic['data'] as $pb)
		{
			$result[] = array(
				0 => $pb['number'] . ' ' . $pb['name'],
				'id' => $pb['id'],
				'number' => $pb['number']
			);
		}
		echo json_encode($result);
		exit;
	}
	
	function actionLossDelete()
	{
		$this->_view['title'] = '删除（操作）';
		$result = Purchase_Loss::delete($_GET['purchase_loss_id']);
		if ($result['ack'] == SUCCESS)
		{
			return $this->_redirectMessage('删除成功', '正在返回..', url('Purchase::Loss/LossList'), $result['ack']);
		}
		else
		{
			return $this->_redirectMessage('删除失败', $result['message'], url('Purchase::Loss/LossList'), $result['ack']);
		}
	}
	
	function actionPurchaseAudit()
	{
		$this->_view['title'] = '审核（操作）';
		if (isset($_GET['purchase_loss_id']))
		{
			$result = Purchase_Loss::purchaseAudit($_GET['purchase_loss_id']);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('审核成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('审核失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
}